﻿#include <QAction>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../process_modules/ui_process.h"
#include "../data_modules/track_data_table_model.h"
#include "../data_modules/track_data.h"
#include "../data_modules/constant_variable.h"
#include "track_view_widget.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , track_widget_(new Track_view_widget(this))
    , ui_process_(new Ui_process(this))
    , model_(new Track_data_table_model(this))
{
    ui->setupUi(this);
    init_widgets();
    init_connects();

    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::sl_filter_from_widgets()
{
    QMap<QString, QVariant>* filter(new QMap<QString, QVariant>());

    if ( !ui->edit_call_sign_->text().isEmpty()) {
        filter->insert(CALL_SIGN, ui->edit_call_sign_->text().trimmed());
    }

    if ( !ui->edit_ssr_->text().isEmpty()) {
        filter->insert(SSR_CODE, ui->edit_ssr_->text().trimmed());
    }

    if ( !ui->spin_max_altitude_->text().isEmpty()) {
        filter->insert(MAX_LATATIUDE, ui->spin_max_altitude_->text().trimmed());
    }

    if ( !ui->spin_min_altitude_->text().isEmpty()) {
        filter->insert(MIN_LATATIUDE, ui->spin_min_altitude_->text().trimmed());
    }

    // 清空View显示内容
    model_->sl_clear_show_list();

    ui_process_->get_filter_conditions(filter, model_->get_track_list());
}

void MainWindow::init_widgets()
{
    file_menu_ = menuBar()->addMenu(tr("File"));

    open_file_action_ = new QAction(tr("打开"), this);
    file_menu_->addAction(open_file_action_);
    split_data_action_ = new QAction(tr("Split data"), this);
    file_menu_->addAction(split_data_action_);
    exit_action_ = new QAction(tr("Exit"), this);
    file_menu_->addAction(exit_action_);

    // Set Model/View
    ui->tableView->setModel(model_);
    ui->scrollArea->setWidget(track_widget_);
}

void MainWindow::init_connects()
{
    connect(open_file_action_, &QAction::triggered,
            ui_process_, &Ui_process::sl_open_files);

    connect(split_data_action_, &QAction::triggered,
            ui_process_, &Ui_process::sl_split_data_to_files);

    connect(exit_action_, &QAction::triggered,
            ui_process_, &Ui_process::sl_exit_application);

    // 用户条件选择航迹
    connect(ui->btn_start_filter_, &QPushButton::clicked,
            this, &MainWindow::sl_filter_from_widgets);

    connect(ui_process_, &Ui_process::si_send_track_to_model,
            model_, &Track_data_table_model::sl_add_track_data);

    connect(ui_process_, &Ui_process::si_clear_model_,
            model_, &Track_data_table_model::sl_clear_show_list);

    connect(ui_process_, &Ui_process::si_updte_model_by_filter,
            model_, &Track_data_table_model::sl_add_filter_data);
}
